﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DemoFacebookAPI
{
    public partial class frmFriendsList : Form
    {
        Thread thread;
        Client cl = new Client();

        List<ChatForm> chat = new List<ChatForm>();
        private frmMain frmMain;
        private Button btnChat;

        //public Form1()
        //{
        //    InitializeComponent();
        //    facebookPass.PasswordChar = '*';

        //    progressBar1.Hide();

        //    this.FormClosing += windowClose;
        //}

        //public frmFriendsList(string strNameChatFace)
        //{
        //    InitializeComponent();
        //    timer1.Enabled = true;
        //    facebookNick.Text = strNameChatFace + "@chat.facebook.com";
        //    facebookPass.PasswordChar = '*';
        //    pictureBox1.Hide();
        //    progressBar1.Hide();
            

        //    this.FormClosing += windowClose;
            
        //}

        //public frmFriendsList(string strNameChatFace, frmMain frmMain)
        //{
        //    InitializeComponent();
        //    this.frmMain = frmMain;
        //    facebookNick.Text = strNameChatFace + "@chat.facebook.com";
        //    facebookPass.PasswordChar = '*';
        //    pictureBox1.Hide();
        //    progressBar1.Hide();


        //    this.FormClosing += windowClose;
            
        //}

        public frmFriendsList(string strNameChatFace, DemoFacebookAPI.frmMain frmMain, Button btnChat)
        {
            InitializeComponent();
            this.frmMain = frmMain;
            this.btnChat = btnChat;
            facebookNick.Text = strNameChatFace + "@chat.facebook.com";
            facebookPass.Text = "";
            facebookPass.PasswordChar = '*';
            pictureBox1.Hide();
            progressBar1.Hide();


            this.FormClosing += windowClose;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadStart threadStart  = new ThreadStart(MainLoop);
            thread = new Thread(threadStart);
            thread.Start();

            FriendsList.Hide();
        }

        private void MainLoop()
        {
            while (true)
            {
                cl.Login();

                cl.Loop();

                for (int i = 0; i < chat.Count; i++)
                {
                    cl.MessageDisplay(chat[i]);   
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // if (Engine.logging == false)
            {
                Engine.facebookNick = facebookNick.Text;

                Engine.facebookPass = facebookPass.Text;

                facebookNick.Hide();
                facebookPass.Hide();
                button1.Hide();
                lblChatFace.Hide();
                lblPass.Hide();
                lblnote1.Hide();
                lblnote2.Hide();
                pictureBox1.Show();
                progressBar1.Show();

                Engine.Loading = true;
                Engine.logging = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            #region login Failed
            if (Engine.loggingFailed)
            {
                FriendsList.Hide();

                facebookNick.Show();
                facebookPass.Show();
                button1.Show();
                lblChatFace.Show();
                lblPass.Show();
                lblnote1.Show();
                lblnote2.Show();
                pictureBox1.Hide();
                progressBar1.Hide();
                

                Engine.loggingFailed = false;
            }
            #endregion

            if (Engine.Loading == true)
            {
                if (progressBar1.Value < 100)
                {
                    progressBar1.Value += 1;
                    Thread.Sleep(100);
                }
                if (progressBar1.Value >= 100)
                {
                    pictureBox1.Hide();
                    progressBar1.Hide();
                    FriendsList.Show();

                    progressBar1.Value = 0;
                    Engine.Loading = false;
                }
            }
            //Refresh dei contatti
            if (FriendsList.Items.Count < Engine.JID_Receiver.Count)
            {
                //FriendsList.Items.Add(Engine.JID_Receiver[Engine.contact_Connected]);

                ParserHtml parserHtml = new ParserHtml();
                Console.WriteLine(Engine.contact_Connected);
                string nick = parserHtml.Parse(Engine.JID_Receiver
                    [Engine.contact_Connected]);
                FriendsList.Items.Add(nick);

                chat.Add(new ChatForm());
                chat[Engine.contact_Connected].setUser
                    (Engine.JID_Receiver[Engine.contact_Connected], nick);

                Engine.contact_Connected++;
            }
            if (Engine.JID_Receiver.Count < FriendsList.Items.Count)
            {
                Console.WriteLine("xem contact: " + Engine.contact_Connected);
                Console.WriteLine("xem list " + FriendsList.Items.Count);
                FriendsList.Items.RemoveAt(Engine.contact_Disconnected);

                chat.RemoveAt(Engine.contact_Disconnected);
                Console.WriteLine("toi dang tru");
                Engine.contact_Connected--;
            }
            //end Refresh
        }

        private void windowClose(object sender, FormClosingEventArgs e)
        {
            thread.Abort();
        }

        private void FriendsList_DoubleClick(object sender, EventArgs e)
        {
            //controllo che i chat client siano attivi
            if (FriendsList.Text == "")
            {
                //do nothing
            }
            else
            {
                chat[FriendsList.SelectedIndex].show();
            }
            //end
        }

        private void frmFriendsList_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Enabled = false;
            //frmMain.Show();
            btnChat.Enabled = true;
        }

        //private void frmFriendsList_FormClosing(object sender, FormClosingEventArgs e)
        //{
        //    Engine.facebookNick = "";
        //    Engine.facebookPass = "";
        //    Engine.Loading = false;
        //    Engine.logging = false;
        //    Engine.logged = false;
        //    Engine.chatRunning = false;
        //    Engine.loggingFailed = false;
        //    Engine.JID_Receiver = new List<string>();
        //    Engine.contact_Connected = 0;
        //    Engine.contact_Disconnected = 0;

        //    //second thread
        //    Engine.temp_JID_Receiver = "";
        //    Engine.SendMessage = false;
        //    Engine.SendMessageBody = "";
        //}

        //private void frmFriendsList_FormClosed(object sender, FormClosedEventArgs e)
        //{
        //    Engine.facebookNick = "";
        //    Engine.facebookPass = "";
        //    Engine.Loading = false;
        //    Engine.logging = false;
        //    Engine.logged = false;
        //    Engine.chatRunning = false;
        //    Engine.loggingFailed = false;
        //    Engine.JID_Receiver = new List<string>();
        //    Engine.contact_Connected = 0;
        //    Engine.contact_Disconnected = 0;

        //    //second thread
        //    Engine.temp_JID_Receiver = "";
        //    Engine.SendMessage = false;
        //    Engine.SendMessageBody = "";
        //}
    }
}
